import 'dart:async';
import 'package:flutter/material.dart';

import '../common/flatPanel.dart';
import '../utils/screw.dart';
import '../models/macItem.dart';

class MacItemDialog extends StatefulWidget {
  MacItemDialog({Key key, this.item}) : super(key: key);

  final MacItem item;

  @override
  _MacItemDialogState createState() => _MacItemDialogState();
}

class _MacItemDialogState extends State<MacItemDialog> {
  final _department = TextEditingController();
  final _name = TextEditingController();
  final _device = TextEditingController();
  final _devices = <String>['手机', '笔记本', '主机'];
  final _mac = TextEditingController();
  final GlobalKey _formKey = new GlobalKey<FormState>();

  @override
  void initState() {
    super.initState();
    _device.text = _devices.first;
    if (widget.item.id != null && widget.item.id != 0) {
      _department.text = widget.item.department;
      _name.text = widget.item.name;
      _device.text = widget.item.device;
      _mac.text = widget.item.mac;
    }
  }

  void _onSubmit() {
    var item = MacItem(
        id: 0,
        department: _department.text,
        name: _name.text,
        device: _device.text,
        mac: _mac.text);
    Navigator.pop(context, item);
  }

  Widget _genDropDownButton() {
    return DropdownButton<String>(
      items: _devices
          .map((e) => DropdownMenuItem<String>(
                value: e,
                child: Text(e),
              ))
          .toList(),
      onChanged: (v) {
        setState(() {
          _device.text = v;
        });
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: MediaQuery.of(context).size.width * 0.6,
      height: MediaQuery.of(context).size.height * 0.6,
      child: FlatPanel(
        child: Form(
          key: _formKey,
          child: Column(
            children: <Widget>[
              TextFormField(
                controller: _department,
                decoration: InputDecoration(
                  hintText: '部门',
                  prefixIcon: Icon(Icons.group)
                ),
                validator: (v) => v.trim().length > 0 ? null : '部门不能为空',
              ),
              TextFormField(
                controller: _name,
                decoration: InputDecoration(
                  hintText: '姓名',
                  prefixIcon: Icon(Icons.person)
                ),
                validator: (v) => v.trim().length > 0 ? null : '姓名不能为空',
              ),

              Row(
                children: <Widget>[
                  Expanded(
                    flex: 3,
                    child: TextFormField(),
                  ),
                  Expanded(
                    child: _genDropDownButton(),
                  )
                ],
              ),
              TextFormField(
                controller: _mac,
                decoration: InputDecoration(
                    hintText: 'Mac地址',
                    prefixIcon: Icon(Icons.tag)
                ),
                validator: (v) => v.trim().length > 0 ? null : 'Mac不能为空',
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: FlatButton(
                        child: Text('清空', style: getTextStyle(fontSize: 18)),
                        onPressed: () {
                          (_formKey.currentState as FormState).reset();
                        }),
                  ),
                  Expanded(
                    flex: 1,
                    child: FlatButton(
                      child: Text(widget.item.id == 0 ? '提交' : '保存',
                          style:
                          getTextStyle(fontSize: 18, color: Colors.blue)),
                      onPressed: () {
                        if ((_formKey.currentState as FormState).validate())
                          _onSubmit();
                      },
                    ),
                  ),
                ],
              )
            ],
          ),
        ),
      ),

    );
  }
}

Future<MacItem> showMacItemDialog(BuildContext context,
{MacItem item}) async{
  return await showDialog<MacItem>(
    context: context,
    builder: (context){
      return Dialog(child: MacItemDialog(item: item));
    }
  );
}
